package com.wannengbang.cloudleader.homepage;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.AgentBean;
import com.wannengbang.cloudleader.bean.UserInfoBean;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.utils.QRCodeUtil;
import com.wannengbang.cloudleader.utils.SPManager;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.ViewLoading;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddAgentActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.iv_erweima)
    ImageView ivErweima;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_mobile)
    EditText editMobile;
    @BindView(R.id.edit_pwd)
    EditText editPwd;
    private UserInfoBean userInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_agent);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null && userInfoBean.getData() != null) {
            Bitmap mBitmap = QRCodeUtil.createQRCodeBitmap(userInfoBean.getData().getInvite_url(), 480, 480);
            ivErweima.setImageBitmap(mBitmap);
        }
    }

    @OnClick(R.id.tv_commit)
    public void onViewClicked() {
        String name = editName.getText().toString();
        String mobile = editMobile.getText().toString();
        String pwd = editPwd.getText().toString();


        if (TextUtils.isEmpty(name)) {
            ToastUtil.showShort("请输入合伙人名称");
            return;
        }
        if (TextUtils.isEmpty(mobile)) {
            ToastUtil.showShort("请输入合伙人手机号");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            ToastUtil.showShort("请输入合伙人密码");
            return;
        }
        ViewLoading.showProgress(mActivity);
        new HomePageModelImpl().requestAgentAdd(mobile, name, pwd, new DataCallBack<AgentBean>() {
            @Override
            public void onSuccessful(AgentBean bean) {
                ToastUtil.showShort(bean.getMsg());
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });

    }
}
